Skip to main content

Crate mq_markdown

Crate mq_markdown 

Source
Expand description

§mq-markdown: Markdown parsing and manipulation for mq

This crate provides comprehensive markdown parsing, manipulation, and conversion functionality used in mq. It offers a robust API to work with markdown content and generate different output formats.

§Features

  • Parse Markdown: Convert markdown strings to structured AST
  • HTML Conversion: Convert between markdown and HTML formats
  • MDX Support: Parse and manipulate MDX (Markdown + JSX) content
  • JSON Export: Serialize markdown AST to JSON (with json feature)
  • Configurable Rendering: Customize output formatting and styles

§Quick Start

§Basic HTML Conversion

use mq_markdown::to_html;

let markdown = "# Hello, world!";
let html = to_html(markdown);
assert_eq!(html, "<h1>Hello, world!</h1>");

§Working with Markdown AST

use mq_markdown::Markdown;

let markdown = "# Heading\n\nParagraph with *emphasis*";
let doc = markdown.parse::<Markdown>().unwrap();

println!("Found {} nodes", doc.nodes.len());
println!("HTML: {}", doc.to_html());
println!("Text: {}", doc.to_text());

§Custom Rendering Options

use mq_markdown::{Markdown, RenderOptions, ListStyle};

let mut doc = "- Item 1\n- Item 2".parse::<Markdown>().unwrap();
doc.set_options(RenderOptions {
    list_style: ListStyle::Plus,
    ..Default::default()
});

// Now renders with "+" instead of "-"
println!("{}", doc);

§Performance Considerations

  • Use &str methods when possible to avoid unnecessary allocations
  • The AST uses structural equality checking for efficient comparisons
  • Consider using CompactString for memory-efficient string storage
  • Position information can be omitted to reduce memory usage

§HTML to Markdown (optional feature)

When the html-to-markdown feature is enabled, this crate can also convert HTML to Markdown.

// This example requires the `html-to-markdown` feature.
// Add `mq-markdown = { version = "...", features = ["html-to-markdown"] }` to your Cargo.toml.
use mq_markdown::convert_html_to_markdown;

let html = "<p>Hello <strong>world</strong>!</p>";
let markdown = convert_html_to_markdown(html)?;
assert_eq!(markdown, "Hello **world**!");

Structs§

Blockquote
Break
Code
CodeInline
ColorTheme
Color theme for rendering markdown nodes with optional ANSI escape codes.
Definition
Delete
Emphasis
Footnote
FootnoteRef
Fragment
Heading
HorizontalRule
Html
Image
ImageRef
Link
LinkRef
List
Markdown
Math
MathInline
MdxFlowExpression
MdxJsEsm
MdxJsxFlowElement
MdxJsxTextElement
MdxTextExpression
Point
Position
RenderOptions
Strong
TableAlign
TableCell
TableRow
Text
Title
Toml
Url
Yaml

Enums§

AttrValue
Represents a typed attribute value that can be returned from or passed to attr/set_attr methods.
ListStyle
Node
TableAlignKind
TitleSurroundStyle
UrlSurroundStyle

Functions§

to_html